using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FinanceStats.Entities;
using FinanceStats.Business;

namespace FinanceStats
{
    public partial class EditItem : Form
    {
        private OutcomeItem outcomeItem;
        private List<OutcomeGroup> outcomeGroups;


        public OutcomeItem OutcomeItem
        {
            get { return outcomeItem; }
            set { outcomeItem = value; }
        }
        
        public List<OutcomeGroup> OutcomeGroups
        {
            get { return outcomeGroups; }
            set { outcomeGroups = value; }
        }
        
        public EditItem()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void ApplyBindings()
        {
            BindingSource bs = this.cmbGroups.DataSource as BindingSource;
            if (bs != null)
            {
                bs.ResetBindings(false);
            }
            else
            {
                bs = new BindingSource();
                bs.DataSource = this.outcomeGroups;
                cmbGroups.DataSource = bs;
                cmbGroups.DisplayMember = "GroupName";
                cmbGroups.ValueMember = "GroupID";
            }
            Binding b = new Binding("Text", this.outcomeItem, "ItemName", false, DataSourceUpdateMode.OnPropertyChanged);
            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add(b);

            b = new Binding("SelectedValue", this.outcomeItem, "GroupID", true, DataSourceUpdateMode.OnPropertyChanged);
            cmbGroups.DataBindings.Clear();
            cmbGroups.DataBindings.Add(b);
            cmbGroups.SelectedValue = outcomeItem.GroupID;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool res = true;
            if (this.outcomeItem.ItemID == null)
            {
                res = DataManager.Instance.InsertOutcomeItem(this.outcomeItem);
            }
            else
            {
                res = DataManager.Instance.UpdateOutcomeItem(this.outcomeItem);
            }
            this.DialogResult = res ? DialogResult.OK : DialogResult.No;
            this.Close();
        }
    }
}